package com.javaee.term.split;

import com.javaee.term.dto.ContactProductDto;
import com.javaee.term.form.ContactForm;
import com.javaee.term.model.Contact;
import com.javaee.term.model.ContactDetail;
import com.javaee.term.model.Product;
import com.javaee.term.model.SendProductList;
import com.javaee.term.service.ProductService;
import com.javaee.term.utils.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.math.BigDecimal;
import java.security.Key;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * author 陈伯瑞
 * date 2019/12/18 0018 23:30
 * version 1.0
 **/

public class ContactFormConver {

    public  Contact fromToContact (ContactForm contactForm,List<Product> products){
        String key=new String();
        if (contactForm.getContactId().isEmpty()) {
            key = KeyUtil.genUniqueKey();
        }else   key=contactForm.getContactId();

        List<ContactProductDto> contactProductDtos = new ArrayList<>();
        //计算合同总金额
        BigDecimal money=new BigDecimal("0.0") ;
        int i = 0;
        for (String productid : contactForm.getProductId()) {
            for(Product product:products)
                if(productid.equals(product.getProductId())) {
                    BigDecimal quantity=new BigDecimal(contactForm.getProductQuantity()[i]) ;
                    money= (product.getProductPrice()).multiply(quantity).add(money);
                    i++;
                }
        }


        Contact contact=new Contact();
        BeanUtils.copyProperties(contactForm,contact);
        contact.setContactId(key);
        contact.setContactMoney(money);

        return  contact;

    }
    public List<ContactDetail> fromToContactDetail(ContactForm contactForm,String contactid){
        List<ContactDetail> contactDetails=new ArrayList<>();
        int i=0;
        for(String productid:contactForm.getProductId()){
            ContactDetail contactDetail=new ContactDetail();

            contactDetail.setProductId(productid);
            contactDetail.setContactId(contactid);
            Integer[] quantityArry=contactForm.getProductQuantity();
            contactDetail.setProductQuantity(quantityArry[i]);
            contactDetail.setDetailId(KeyUtil.genUniqueKey());
            contactDetails.add(contactDetail);
            i++;
        }
           return contactDetails;

    }
}
